﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using Libs.Db;
using Libs.Configs;

namespace Libs.Security
{
    public class UserLog
    {
        public int LogID { get; set; }
        public int UserID { get; set; }
        public string UserName { get; set; }
        public string Url { get; set; }
        public string Task { get; set; }
        public string Description { get; set; }
        public string Content { get; set; }
        public DateTime LogTime { get; set; }
        public int ReturnValue { get; set; }

        public UserLog()
        {

        }

        ~UserLog()
        {

        }

        /// <summary>
        /// Thêm mới log
        /// </summary>
        public void Add()
        {
            DbHelper db = new DbHelper(Config.SecurityConnectionStrings);
            SqlParameter[] pars = new SqlParameter[7];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@UserID", UserID);
            pars[2] = new SqlParameter("@UserName", UserName);
            pars[3] = new SqlParameter("@Url", Url);
            pars[4] = new SqlParameter("@Task", Task);
            pars[5] = new SqlParameter("@Description", Description);
            pars[6] = new SqlParameter("@Content", Content == null ? "" : Content);

            db.ExecuteNonQuerySP("sp_UserLog_Insert", pars);
            UserID = Convert.ToInt32(pars[0].Value);
        }

        public DataTable GetTable(int userID, int pageIndex, int pageSize, ref int totalRecord)
        {
            DbHelper db = new DbHelper(Config.NewsConnectionStrings);
            SqlParameter[] pars = new SqlParameter[4];
            pars[0] = new SqlParameter("@TotalRecord", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = userID == 0 ? new SqlParameter("@UserID", DBNull.Value) : new SqlParameter("@UserID", userID);
            pars[2] = new SqlParameter("@PageIndex", pageIndex);
            pars[3] = new SqlParameter("@PageSize", pageSize);

            DataTable dt = db.GetDataTableSP("sp_UserLog_SelectListPage", pars);
            totalRecord = Convert.ToInt32(pars[0].Value);
            return dt;
        }

    }
}
